<template>
    <div
        class="hover-tip-warp"
        :class="position"
        @mouseenter="mouseEnter"
        @mouseleave="mouseLeave"
    >
        <slot></slot>
        <div class="hover-tip-container" :class="hoverClass">
            <span :style="getStrStyle">{{ title }}</span>
        </div>
    </div>
</template>
<script>
export default {
    name: "hover-tip",
    props: {
        title: {
            default: "title",
            type: String,
        },
        position: {
            default: "top",
            type: String,
        },
        width: {
            default: "auto",
            type: String,
        },
        align: {
            default: "center",
            type: String,
        },
    },
    data() {
        return {
            hoverClass: "",
            timer: null,
            hover_time: 200,
        };
    },
    computed: {
        getStrStyle() {
            let arr = [];
            arr.push("width:" + this.width);
            arr.push("width:" + this.width);
        },
    },
    methods: {
        mouseEnter() {
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                this.hoverClass = "show";
            }, this.hover_time);
        },
        mouseLeave() {
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                this.hoverClass = "hide";
            }, this.hover_time);
        },
    },
};
</script>
<style lang="scss" scoped>
@keyframes show {
    0% {
        opacity: 0;
        z-index: auto;
    }
    100% {
        opacity: 1;
        z-index: auto;
    }
}
@keyframes hide {
    0% {
        opacity: 1;
        z-index: auto;
    }
    99% {
        opacity: 0;
        z-index: auto;
    }
    100% {
        opacity: 0;
        z-index: -1;
    }
}
.show {
    animation: show 0.3s forwards;
}
.hide {
    animation: hide 0.3s forwards;
}
.hover-tip-warp {
    position: relative;
    &.bottom {
        .hover-tip-container {
            bottom: -9px;
            left: 50%;
            transform: translate(-50%, 100%);
            ::before {
                top: 0px;
                left: 50%;
                transform: rotate(45deg) translate(-50%);
            }
        }
    }
    &.top {
        .hover-tip-container {
            top: -9px;
            left: 50%;
            transform: translate(-50%, -100%);
            ::before {
                bottom: -9px;
                left: 50%;
                transform: rotate(45deg) translate(-50%);
            }
        }
    }
    &.left {
        .hover-tip-container {
            top: 50%;
            left: -12px;
            transform: translate(-100%, -50%);
            ::before {
                top: 50%;
                right: 0;
                transform: rotate(45deg) translate(0, -50%);
            }
        }
    }
    &.right {
        .hover-tip-container {
            top: 50%;
            right: -12px;
            transform: translate(100%, -50%);
            ::before {
                top: 50%;
                left: -9px;
                transform: rotate(45deg) translate(0, -50%);
            }
        }
    }
    .hover-tip-container {
        transition: opacity 0.2s;
        opacity: 0;
        z-index: -1;
        display: flex;
        align-content: center;
        justify-content: center;
        position: absolute;
        padding: 8px 12px;
        background: #000000;
        box-shadow: 0px 6px 9px 0px rgba(0, 0, 0, 0.2);
        border-radius: 5px;
        font-size: 14px;

        font-weight: 600;
        color: #ffffff;
        span {
            text-align: center;
            width: auto;
            white-space: pre-wrap;
        }
        ::before {
            content: "";
            width: 12px;
            height: 12px;
            background: #000;
            border-radius: 3px;
            display: block;
            position: absolute;
        }
    }
}
</style>
